package com.fjc.service;

import com.fjc.base.ResponseObject;
import com.fjc.dao.UserMapper;
import com.fjc.domain.User;
import com.fjc.domain.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by fjc on 2018/8/29.
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public void createUser(User user){
        userMapper.insertSelective(user);
    }

    public ResponseObject login(User user, HttpServletRequest request){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        List<User> list = userMapper.selectByExample(userExample);
        if(!(list.size() > 0)){
            return new ResponseObject(-1,"该账号不存在");
        }else if(!list.get(0).getPassword().equals(user.getPassword())){
            return new ResponseObject(-1,"账号或者 密码错误");
        }
        request.getSession().setAttribute("user",user);
        return new ResponseObject(00,"登陆成功");
    }
}
